package com.css.bjdt_core.executive.report;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.nutz.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 报表缓存
 */
public class ReportCache {

	private static class SingletonHolder {
		private static final ReportCache INSTANCE = new ReportCache();
	}

	public static final ReportCache getInstance() {
		return SingletonHolder.INSTANCE;
	}

	private ConcurrentMap<String, Map<String, Object>> cache = new ConcurrentHashMap<String, Map<String, Object>>();
	private ConcurrentHashMap<String, Long> autoGenCache = new ConcurrentHashMap<String, Long>();

	public final static String CELL_CACHE = "cellCache";
	public final static String CELLDATA_CACHE = "cellDataCache";
	public final static String VALIDATE_CACHE = "validateCache";
	public final static String AREA_CACHE = "areaCache";
	public final static String CALC_CACHE = "calcCache";
	//放所有CELL用的
	public final static String VALIDATE_CELL_CACHE = "validateCellCache";

	@Autowired
	private Dao dao;

	private ReportCache() {
		//预先初始化3块缓存
		Map<String, Object> cellCache = new ConcurrentHashMap<String, Object>();
		Map<String, Object> cellDataCache = new ConcurrentHashMap<String, Object>();
		Map<String, Object> validateCache = new ConcurrentHashMap<String, Object>();
		Map<String, Object> areaCache = new ConcurrentHashMap<String, Object>();
		Map<String, Object> calcCache = new ConcurrentHashMap<String, Object>();
		Map<String, Object> validateCellCache = new ConcurrentHashMap<String, Object>();
		cache.put(CELL_CACHE, cellCache);
		cache.put(CELLDATA_CACHE, cellDataCache);
		cache.put(VALIDATE_CACHE, validateCache);
		cache.put(AREA_CACHE, areaCache);
		cache.put(CALC_CACHE, calcCache);
		cache.put(VALIDATE_CELL_CACHE, validateCellCache);
	}

	public void clear() {
		getCache(CELL_CACHE).clear();
		getCache(CELLDATA_CACHE).clear();
		getCache(VALIDATE_CACHE).clear();
		getCache(AREA_CACHE).clear();
		getCache(CALC_CACHE).clear();
		getCache(VALIDATE_CELL_CACHE).clear();
		cache.clear();
		autoGenCache.clear();
	}

	public Object getCell(String key) {
		return get(CELL_CACHE, key);
	}

	public void putCell(String key, Object value) {
		put(CELL_CACHE, key, value);
	}

	public void clearCell(String key) {
		clear(CELL_CACHE, key);
	}

	public Object getCellData(String key) {
		return get(CELLDATA_CACHE, key);
	}

	public void putCellData(String key, Object value) {
		put(CELLDATA_CACHE, key, value);
	}

	public void clearCellData(String key) {
		clear(CELLDATA_CACHE, key);
	}

	public Object get(String cacheName, String key) {
		Map<String, Object> temp = cache.get(cacheName);
		if (temp == null) {
			return null;
		}
		return temp.get(key);
	}

	public void put(String cacheName, String key, Object value) {
		Map<String, Object> temp = cache.get(cacheName);
		if (temp == null) {
			temp = new ConcurrentHashMap<String, Object>();
			cache.putIfAbsent(cacheName, temp);
		}
		((ConcurrentHashMap<String, Object>) temp).putIfAbsent(key, value);
	}

	public long getAutoGenTime(String key) {
		Long res = autoGenCache.get(key);
		return res == null ? 0l : res;
	}

	public void putAutoGenTime(String key, long time) {
		autoGenCache.putIfAbsent(key, time);
	}

	public void clear(String cacheName, String key) {
		Map<String, Object> temp = cache.get(cacheName);
		if (temp == null) {
			return;
		}
		temp.remove(key);
	}

	public Map<String, Object> getCache(String cacheName) {
		return cache.get(cacheName);
	}
}
